import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { HttpException, HttpStatus } from '@nestjs/common';
import CommonUtil from '../tool/CommonUtil';

export class UserUnauthorizedException extends HttpException {
  constructor(msg = '登录已失效，请重新登录！') {
    super(msg, HttpStatus.OK);
  }
}

@Catch(UserUnauthorizedException)
export class UserUnauthorizedExceptionFilter implements ExceptionFilter {
  catch(exception: UserUnauthorizedException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();

    response.status(200).json(CommonUtil.returnError(exception.message, 8000));
  }
}
